package com.chen.spring.cloud.miniapp.api.feign.controller;

import com.chen.entity.User;
import com.chen.spring.cloud.miniapp.api.feign.service.UserService;
import com.chen.util.MapperUtils;
import com.chen.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/signIn")
    public R signIn(String wx_openid,String r_id){
        if (StringUtils.isEmpty(wx_openid)){
            return R.error(403,"没有登录");
        }
        if (StringUtils.isEmpty(r_id)){
            return R.error(500,"缺少参数");
        }

        String userjson = userService.getUidByOpenid(wx_openid);
        try {
            User user = MapperUtils.json2pojo(userjson, User.class);
            String si_id = userService.signIn(r_id,user.getU_id());
            Map<String,Object> result = new HashMap<>();
            result.put("data",si_id);
            return R.ok(result);
        } catch (Exception e){
            e.printStackTrace();
        }
        return R.error(500,"稍后再试！");
    }
}
